const excelPort = require('excel-export')
const { writeFile } = require('fs')
const conf = {
  cols: [
    {
      caption: '模块名',
      type: 'string',
      width: 60,
    },
    {
      caption: '文件名',
      type: 'string',
      width: 60,
    },
    {
      caption: '字段名',
      type: 'string',
      width: 80,
    },
    {
      caption: '中文',
      type: 'string',
      width: 240,
    },
    {
      caption: '英文',
      type: 'string',
      width: 340,
    },
  ],
  rows: [],
}

const write = (datas, excelName = 'i18n') => {
  for (let i = 0; i < datas.length; i++) {
    const item = datas[i]
    console.log('-------', 'item', item.name)

    for (let index = 0; index < item.list.length; index++) {
      const one = item.list[index]
      console.log('-------', 'one', one.fileName)

      for (let indx = 0; indx < one.datas.length; indx++) {
        const data = one.datas[indx]
        conf.rows.push([item.name, one.fileName, data.key, data.zh, data.en])
      }
    }
  }

  const result = excelPort.execute(conf)
  const excelUri = `./outFile/${excelName}.xlsx`

  writeFile(excelUri, result, 'binary', err => {
    if (err) {
      console.error('-------', 'write Error: ', err)
    } else {
      console.log('-------', 'write success')
    }
  })
}

module.exports = write
